<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机主名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入机主名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份" prop="UIdentity">
        <el-select v-model="queryParams.UIdentity" placeholder="请选择身份" clearable style="width: 200px">
          <el-option
              v-for="dict in wxz_order_identity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input
            v-model="queryParams.studentId"
            placeholder="请输入学号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宿舍栋数" prop="building">
        <el-select v-model="queryParams.building" placeholder="请选择宿舍栋数" clearable style="width: 200px">
          <el-option
              v-for="dict in wxz_order_building"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍号" prop="dormitory">
        <el-input
            v-model="queryParams.dormitory"
            placeholder="请输入宿舍号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择订单类型" clearable style="width: 200px">
          <el-option
              v-for="dict in wxz_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单详情" prop="details">
        <el-input
            v-model="queryParams.details"
            placeholder="请输入订单详情"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修人员" prop="nickName">
        <el-input
            v-model="queryParams.nickName"
            placeholder="请输入维修人员"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完成订单" prop="completionTimeRange">
        <el-date-picker
            clearable
            v-model="queryParams.completionTimeRange"
            type="datetimerange"
            value-format="YYYY-MM-DD hh:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format=" hh:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wxz:history:export']"
        >导出全部
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Document"
            @click="handleGD"
            v-hasRole="['club_leader']"
        >归档
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55" align="center"/>
      <el-table-column label="机主名称" align="center" prop="name"/>
      <el-table-column label="身份" align="center" width="65" prop="UIdentity">
        <template #default="scope">
          <dict-tag :options="wxz_order_identity" :value="scope.row.UIdentity"/>
        </template>
      </el-table-column>
      <el-table-column label="宿舍地址" align="center" prop="building">
        <template #default="scope">
          {{ scope.row.building }}#{{ scope.row.dormitory }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="订单类型" align="center" width="80" prop="type">
        <template #default="scope">
          <dict-tag :options="wxz_order_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wxz_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="维修人员" align="center" width="80" prop="nickName"/>
      <el-table-column label="完成订单时间" align="center" prop="completionTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="More" @click="handleOrderDesc(scope.row)"
                     v-hasPermi="['wxz:history:list']">
            更多
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="History">
import {listHistory, getHistory, delHistory, addHistory, updateHistory, archiveOrder} from "@/api/wxz/history.js";
import {userOrderState} from "@/store/index.js"

const router = useRouter()

const orderState = userOrderState();
const {proxy} = getCurrentInstance();
const {
  wxz_order_type,
  wxz_order_identity,
  wxz_order_status,
  wxz_order_building
} = proxy.useDict('wxz_order_type', 'wxz_order_identity', 'wxz_order_status', 'wxz_order_building');

const historyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    UIdentity: null,
    studentId: null,
    building: null,
    dormitory: null,
    phone: null,
    type: null,
    details: null,
    status: null,
    staffId: null,
    nickName: null,
    completionTimeRange: null,
    assignorId: null,
    archiveTime: null,
  },
  rules: {
    name: [
      {required: true, message: "机主名称不能为空", trigger: "blur"}
    ],
    UIdentity: [
      {required: true, message: "身份不能为空", trigger: "blur"}
    ],
    studentId: [
      {required: true, message: "学号不能为空", trigger: "blur"}
    ],
    building: [
      {required: true, message: "宿舍栋数不能为空", trigger: "blur"}
    ],
    dormitory: [
      {required: true, message: "宿舍号不能为空", trigger: "blur"}
    ],
    phone: [
      {required: true, message: "手机号不能为空", trigger: "blur"}
    ],
    type: [
      {required: true, message: "订单类型不能为空", trigger: "change"}
    ],
    details: [
      {required: true, message: "订单详情不能为空", trigger: "blur"}
    ],
    status: [
      {required: true, message: "订单状态不能为空", trigger: "change"}
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询历史订单列表 */
function getList() {
  loading.value = true;
  listHistory(queryParams.value).then(response => {
    historyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    console.log(historyList.value)
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    UIdentity: null,
    studentId: null,
    building: null,
    dormitory: null,
    phone: null,
    type: null,
    details: null,
    status: null,
    staffId: null,
    createTime: null,
    updateTime: null,
    nickName: null,
    updateBy: null,
    completionTime: null,
    assignorId: null,
    remark: null,
    delFlag: null,
    archiveTime: null
  };
  proxy.resetForm("historyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加历史订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getHistory(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改历史订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["historyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateHistory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addHistory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除历史订单编号为"' + _ids + '"的数据项？').then(function () {
    return delHistory(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 获取订单更多信息 */
async function handleOrderDesc(row) {
  await orderState.setOrder(row)
  await router.push(`/wxz/desc/order`);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wxz/history/export', {
    ...queryParams.value
  }, `history_${new Date().getTime()}.xlsx`)
}

const handleGD = async () => {
  proxy.$modal.confirm('是否对数据进行归档处理').then(function () {
    return archiveOrder()
  }).then((res) => {
    getList();
    proxy.$modal.msgSuccess(`已成功归档${res.data}条数据`);
  }).catch(() => {
  });

}

getList();
</script>
